package com.edu.internship.busi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.edu.internship.busi.dto.CardDTO;
import com.edu.internship.busi.entity.Card;
import com.edu.internship.busi.entity.Student;
import com.edu.internship.busi.service.ICardService;
import com.edu.internship.busi.service.IStudentService;
import com.edu.internship.glabal.constants.ResultCodeEnum;
import com.edu.internship.glabal.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * @author LPCLYP
 * @date
 */
@RestController
@RequestMapping("api/card")
@Slf4j
@Api(tags = {"卡管理"})
public class CardController {

    @Resource
    private ICardService cardService;
    @Resource
    private IStudentService studentService;


    @GetMapping("/getAll")
    public List<Card> getAll(){
        return cardService.list();
    }
    /**
     * 新增卡片
     * @param dto
     */
    @PostMapping("/createCard")
    @ApiOperation(value = "新增-Card",notes = "新增-Card")
    public void createCard(@Validated @RequestBody CardDTO dto) {

        LambdaQueryWrapper<Student> stQw = Wrappers.lambdaQuery();
        stQw.eq(Student::getStudentNumber,dto.getStudentId());
        List<Student> list = studentService.list(stQw);
        log.info("学生:{}",list);
        if (ObjectUtils.isEmpty(list)) {
            log.info("数据:{}","无法添加");
            throw new BusinessException(ResultCodeEnum.BAD_REQUEST,"不存在此学号的学生");
        } else {
            cardService.createCard(dto);
        }

    }

    @GetMapping("/searchCard")
    @ApiOperation(value = "查询-Card" ,notes = "查询-Card")
    public List<Card> searchCard(CardDTO dto) {
        LambdaQueryWrapper<Card> cardQw = Wrappers.lambdaQuery();
        cardQw.eq(Card::getCardId,dto.getCardId());
        return cardService.list(cardQw);
    }
    /**
     * 更新卡信息
     * @param dto
     */
    @PostMapping("/updateCard")
    @ApiOperation(value = "更新-Card",notes = "更新-Card")
    public void updateCard(@Validated @RequestBody CardDTO dto) {
        cardService.updateCard(dto);
    }

    /**
     * 根据主键列表批量删除
     * @param ids
     */
    @PostMapping("removeCard")
    @ApiOperation(value = "批量删除-Card",notes = "批量删除-Card")
    public void removeCard(@Validated @RequestBody List<String> ids) {
        cardService.removeCard(ids);
    }
}
